package com.example.demo.web;

import com.example.demo.entity.QuestionCategory;
import com.example.demo.param.dto.category.InsertCategoryDto;
import com.example.demo.param.dto.category.UpdateCategoryDto;
import com.example.demo.param.vo.CategoryVO;
import com.example.demo.service.ICateGoryService;
import com.example.demo.service.ITokenService;
import com.example.demo.util.Json;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "问题分类")
@RestController
@RequestMapping("questionCategory")
public class CategoryController extends BaseController{
    @Autowired private ICateGoryService cateGoryService;
    @Autowired private ITokenService tokenService;

    @ApiOperation("新增分类")
    @PostMapping("insert")
    public Json<Boolean> insert(@RequestBody InsertCategoryDto dto, HttpServletRequest request){
        tokenService.isRoleType(dto.getToken(),2);
        Boolean exit = this.cateGoryService.insert(dto.getTitle(),dto.getImg(), request);
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("编辑分类")
    @PutMapping("update")
    public Json<Boolean> update(@RequestBody UpdateCategoryDto dto, HttpServletRequest request){
        tokenService.isRoleType(dto.getToken(),2);
        Boolean exit = this.cateGoryService.update(dto.getId(),dto.getTitle(), dto.getImg(), request);
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("删除分类")
    @DeleteMapping("delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String")
    })
    public Json<Boolean> delete(@RequestParam String id, @RequestParam String token){
        tokenService.isRoleType(token,2);
        Boolean exit = this.cateGoryService.delete(id);
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("查询一条分类信息")
    @GetMapping("getById/{id}")
    public Json<QuestionCategory> getById(@ApiParam(name = "id", value = "问题ID", required = true) @PathVariable("id") String id){
        return new Json<QuestionCategory>(this.cateGoryService.getById(id));
    }

    @ApiOperation("查询全部分类信息")
    @GetMapping("getAll")
    public Json<List<QuestionCategory>> getAll(){
        return new Json<List<QuestionCategory>>(this.cateGoryService.getAll());
    }
}
